﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DYSales.Views;
using C1.Silverlight.DataGrid.Filters;
using C1.Silverlight.DataGrid.Summaries;
using System.IO;
using System.Text;
using System.ServiceModel.DomainServices.Client;

namespace DYSales
{
    public class other
    {
        static other ot;
        internal static other GetOther()
        {
            if (ot == null)
            {
                ot = new other();
            }
            return ot;
        }

        internal static bool o_自动打印小票 = true;

        internal void dg_AutoGeneratingColumn(object sender, C1.Silverlight.DataGrid.DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Property.PropertyType.Equals(typeof(double)))
            {
                e.Column.Format = "0.##";
            }
            else if (e.Property.PropertyType.Equals(typeof(decimal))) 
            {
                e.Column.Format = "0.##";
                DataGridAggregate.SetAggregateFunctions(e.Column, new DataGridAggregatesCollection { new DataGridAggregateSum() });
            }
            if (e.Property.PropertyType.Equals(typeof(DateTime)))
            {
                e.Column.Format = "yyyy/MM/dd HH:mm:ss";
            }
            #region 减少列
            string headername = e.Column.Header.ToString();
            switch (headername)
            {
                // Cancel the columns you don't want to generate.
                //case "Effect":
                //case "Clip":
                //case "操作时间":
                case "ID":
                case "外键":
                //case "导入时间":
                    e.Cancel = true;
                    break;

                ////自定义过滤工具
                //case "通信费到期日":
                //    DataGridContentFilter filter = new DataGridContentFilter
                //    {
                //        Content = new DataGridFilterList { Items = { new DataGridMultiLineDateTimeFilter() } }
                //    };
                //    e.Column.Filter = filter;
                //    break;
                //自定义显示格式
                //case "交易时间":
                //    e.Column.Format = "yyyy/MM/dd HH:mm:ss";
                //    break;

                //// Update column headers when generating.
                //case "费率":
                //    Binding bind = new Binding("c_费率");
                //    bind.StringFormat = @"{0:0.00%}";
                //    e.Column.Binding = bind;
                //    break;

                //case "RenderSize":

                //    e.Column.Header = "Rendered Size";
                //    break;
            }
            #endregion
        }

        internal void dg_RejectChanges(C1.Silverlight.DataGrid.C1DataGrid dg)
        {
            if (dm.getDS().HasChanges)
            {
                dm.getDS().RejectChanges();
                dg.CancelEdit();
                foreach (var item in dg.Rows)
                {
                    item.Validate();
                }
            }
        }

        internal void dg_CommittedRowEdit(object sender, C1.Silverlight.DataGrid.DataGridRowEventArgs e)
        {
            e.Row.Validate();
        }

        internal void GoUrl(Uri url)
        {
            if (!App.Current.IsRunningOutOfBrowser)
            {
                HtmlPage.Window.Navigate(url, "_blank");
            }
            else
            {
                HyperlinkButton link = new HyperlinkButton();
                link.NavigateUri = url;
                HyperlinkButtonAutomationPeer hyperlinkButtonAutomationPeer
                  = new HyperlinkButtonAutomationPeer(link);
                hyperlinkButtonAutomationPeer.RaiseAutomationEvent
                (AutomationEvents.InvokePatternOnInvoked);
                IInvokeProvider iprovider = (IInvokeProvider)hyperlinkButtonAutomationPeer;
                if (iprovider != null)
                    iprovider.Invoke();
            }
        }

        internal static bool checkInt(string obj)
        {
            int o;
            if (int.TryParse(obj, out o))
            {
                return true;
            }
            return false;
        }

        internal static bool checkDate(string obj)
        {
            DateTime o;
            if (DateTime.TryParse(obj, out o))
            {
                return true;
            }
            return false;
        }

        internal static bool checkDecimal(string obj)
        {
            decimal o;
            if (decimal.TryParse(obj, out o))
            {
                return true;
            }
            return false;
        }

        internal static bool checkDouble(string obj)
        {
            double o;
            if (double.TryParse(obj, out o))
            {
                return true;
            }
            return false;
        }

        static cwin_Question cwin;
        internal static cwin_Question GetMessageBox()
        {
            if (cwin == null)
            {
                cwin = new cwin_Question();
                return cwin;
            }
            return cwin;
        }

      

    }
}
